<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="80%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="公司名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="公司名称"></el-input>
    </el-form-item>
    <el-form-item label="公司地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="公司地址"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="telephone">
      <el-input v-model="dataForm.telephone" placeholder="手机号"
                :rules="rulesUtils.checkPhone"></el-input>
    </el-form-item>
    <el-form-item label="传真" prop="fax">
      <el-input v-model="dataForm.fax" placeholder="传真"></el-input>
    </el-form-item>
    <el-form-item label="邮编" prop="postcode">
      <el-input v-model="dataForm.postcode" placeholder="邮编"></el-input>
    </el-form-item>
<!--    <el-form-item label="启用时间" prop="begintime">-->
<!--      <el-input v-model="dataForm.begintime" placeholder="启用时间"></el-input>-->
<!--    </el-form-item>-->

      <el-form-item label="启用时间" prop="begintime">
        <el-date-picker type="date" v-model="dataForm.begintime" @on-change="_begintimeChange"
                        placeholder="启用时间" :editable="false" style="width: 100%"></el-date-picker>
      </el-form-item>
<!--    <el-form-item label="到期时间" prop="endtime">-->
<!--      <el-input v-model="dataForm.endtime" placeholder="到期时间"></el-input>-->
<!--    </el-form-item>-->
      <el-form-item label="到期时间" prop="endtime">
        <el-date-picker type="date" v-model="dataForm.endtime" @on-change="_endtimeChange"
                        placeholder="到期时间" :editable="false" style="width: 100%"></el-date-picker>
      </el-form-item>
    <el-form-item label="本位币" prop="moneyUnit">
      <el-input v-model="dataForm.moneyUnit" placeholder="本位币"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import rules, {checkPhoneNumber} from '../../../utils/validate'

export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          address: '',
          telephone: '',
          fax: '',
          postcode: '',
          begintime: '',
          endtime: '',
          moneyUnit: ''
        },
        dataRule: {
          name: [
            { required: true, message: '公司名称不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '公司地址不能为空', trigger: 'blur' }
          ],
          telephone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' },
            { required: true, validator: checkPhoneNumber, trigger: 'blur' }
          ],
          // fax: [
          //   { required: true, message: '传真不能为空', trigger: 'blur' }
          // ],
          // postcode: [
          //   { required: true, message: '邮编不能为空', trigger: 'blur' }
          // ],
          begintime: [
            { required: true, message: '启用时间不能为空', trigger: 'blur' }
          ],
          endtime: [
            { required: true, message: '到期时间不能为空', trigger: 'blur' }
          ],
          moneyUnit: [
            { required: true, message: '本位币不能为空', trigger: 'blur' }
          ]
        },
        rulesUtils: rules

      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sto/tenantinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.tenantInfo.name
                this.dataForm.address = data.tenantInfo.address
                this.dataForm.telephone = data.tenantInfo.telephone
                this.dataForm.fax = data.tenantInfo.fax
                this.dataForm.postcode = data.tenantInfo.postcode
                this.dataForm.begintime = data.tenantInfo.begintime
                this.dataForm.endtime = data.tenantInfo.endtime
                this.dataForm.moneyUnit = data.tenantInfo.moneyUnit
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sto/tenantinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'address': this.dataForm.address,
                'telephone': this.dataForm.telephone,
                'fax': this.dataForm.fax,
                'postcode': this.dataForm.postcode,
                'begintime': this.dataForm.begintime,
                'endtime': this.dataForm.endtime,
                'moneyUnit': this.dataForm.moneyUnit
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },

      _begintimeChange (data) {
        this.dataForm.begintime = new Date(data)
      },
      _endtimeChange (data) {
        this.dataForm.endtime = new Date(data)
      }
    }
  }
</script>
